﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.QingJia;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.QingJia
{
    public class ShenPiCreateCommandHandler : IRequestHandler<ShenPiCreateCommand, APIResult<object>>
    {
        private readonly IBaseRepository<LeaveApplicationForm> lRepo;
        private readonly IBaseRepository<RecordModel> rRepo;

        public ShenPiCreateCommandHandler(IBaseRepository<LeaveApplicationForm> lRepo, IBaseRepository<RecordModel> rRepo)
        {
            this.lRepo = lRepo;
            this.rRepo = rRepo;
        }



        public async Task<APIResult<object>> Handle(ShenPiCreateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            try
            {
                // 1. 获取现有的请假申请
                var application = lRepo.GetAll().Result
                    .FirstOrDefault(x => x.FormId == request.FormId);

                if (application == null)
                {
                    result.Code = APIEnum.操作失败;
                    result.Msg = "未找到对应的请假申请";
                    return result;
                }

                

                // 2. 更新申请状态
                if (request.ApprovalOpinion == "拒绝")
                {
                    application.Status = Status.已完成不通过; // 审批不通过，直接设置为已完成
                    application.Reson = request.ApproveReson;
                }
                else
                {
                   if(request.HaoId==1)
                    {
                        application.Status = Status.已审核;
                    }
                   else if(request.HaoId==2)
                    {
                        application.Status=Status.已完成通过;
                    }
                }
                // 3. 更新申请记录
                await lRepo.Update(application);
                // 4. 创建审批记录
                var record = new RecordModel
                {
                    FormId = request.FormId,//申请表id
                    DepartId = request.DepartId,//部门
                    ShenPiId = request.ShenPiId,//审批人id（登录人）
                    ShenPiName = request.ShenPiName,//审批人姓名（登录人）
                    Reason = request.Reason,//请假事由
                    Createtime = DateTime.Now,//审批时间
                    Status = application.Status,//审批状态
                    HaoId = request.HaoId,
                    ApprovalOpinion = request.ApprovalOpinion,
                    ApproveReson = request.ApproveReson
                };

                await rRepo.Add(record);
                

                result.Code = APIEnum.操作成功;
                result.Msg = "审批操作已完成";
            }
            catch (Exception ex)
            {
                result.Code = APIEnum.操作失败;
                result.Msg = $"审批过程中发生错误：{ex.Message}";
                // 记录日志
                // _logger.LogError(ex, "审批流程异常 LeaveId:{LeaveId}", request.LeaveId);
            }

            return await Task.FromResult(result);
        



    }




    }
}
